package com.hz.Tech2Real.yeyouwang.web;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hz.Tech2Real.yeyouwang.service.loginService;
import com.hz.Tech2Real.yeyouwang.service.Impl.loginServiceImpl;

public class Tech2RealServlet extends HttpServlet {

	private static final long serialVersionUID = -8768776864908780894L;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");

		// String Userlogin = request.getParameter("Userlogin");
		// String Password = request.getParameter("Password");
		String action = request.getParameter("action");
		if (null == action || "".equals(action) || "login".equals(action)) {
			login(request, response);
		}

	}

	private void login(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		loginService d = new loginServiceImpl();
		String Phone = request.getParameter("UserPhone");
		String PassWord = request.getParameter("password");
		Map<String, Object> map = d.login(Phone, PassWord);

		if (map.isEmpty()) {
			response.getWriter().write("<script type=\"text/javascript\">alert(\"账号或密码错误！\");location.href=\"login.jsp\"</script>");
//			request.getRequestDispatcher("chengg.jsp").forward(request, response);
//			request.getRequestDispatcher("chengg.jsp").forward(request, response);
		}else{
			HttpSession session = request.getSession();
			session.setAttribute("phone", map);
			response.getWriter().write("<script type=\"text/javascript\">alert(\"登入成功！\");location.href=\"http://192.168.50.125:8080/Tech2Real/homePageAction\"</script>");		
		
		}
	}

}
